//
//  AATRaderIndicatorView.swift
//  StringForSwift
//
//  Created by DeLong Yang on 2017/7/25.
//  Copyright © 2017年 YiXue. All rights reserved.
//

import UIKit

class AATRaderIndicatorView: UIView {

    
    
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
        
        let  context = UIGraphicsGetCurrentContext()  // 获取当前的画布
        
        // move to the cente
       
        let  width:CGFloat = rect.size.width
        let  height:CGFloat = rect.size.height
        var  length:CGFloat = 0.0
        if width>=height {
            length =  height
        }else{
            length = width
        }
        
        let radius = 0.45 * length
        
        
        UIGraphicsGetCurrentContext()?.translateBy(x: width/2.0, y: height/2.0)
        // swift  we use
        UIGraphicsGetCurrentContext()?.setLineWidth(0.1);
        UIGraphicsGetCurrentContext()?.setStrokeColor(UIColor.white.cgColor)
        
        
        let single:CGFloat = CGFloat(Double.pi/180/2.0);
        let colorAlpha :CGFloat = CGFloat(Double.pi/90/2)
        var x:CGFloat =  0.0
        //
        for _ in 1...180{
            context?.move(to: CGPoint(x: 0, y: 0))
            context?.addArc(center: CGPoint(x:0,y:0), radius: radius, startAngle: 0, endAngle: -single, clockwise: true)
            
            //
            x = x + colorAlpha;
            let alpha:CGFloat = sin(1-x)
            let color = UIColor(colorLiteralRed: 102.0/255.0, green: 102.0/255.0, blue: 102.0/255.0, alpha: Float(alpha))
            color.setFill()
            
            UIGraphicsGetCurrentContext()?.fillPath()
            color.setStroke()
            UIGraphicsGetCurrentContext()?.drawPath(using: .fill)
            UIGraphicsGetCurrentContext()?.rotate(by: -single)
            
            
        }
    }
 

}
